package com.iotek.ssm.controller;

import com.iotek.ssm.pojo.*;
import com.iotek.ssm.service.ApplyService;
import com.iotek.ssm.service.ManagerService;
import com.iotek.ssm.service.SectionService;
import com.iotek.ssm.service.UserService;
import com.iotek.ssm.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import sun.security.provider.MD5;

import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/user")
public class UserController {
    private static final String FORWARD = "forward:";
    private static final String REDIRECT = "redirect:";

    @Autowired
    private UserService userService;

    @Autowired
    private SectionService sectionService;

    @Autowired
    private ApplyService applyService;

    @Autowired
    private Util util;

    //ajax检测登录和注册页面phone用户名是否可用   0：不存在 1：已存在  -1：异常
    @RequestMapping("/checkPhone")
    @ResponseBody
    public int checkPhone(String phone) {
        System.out.println("************phone=" + phone);
        try {
            User user = userService.findByPhone(phone);
            if (user != null)
                return 1;
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    //用户注册操作
    @RequestMapping("/register")
    public String register(Model model, HttpServletRequest req, User user) {
        System.out.println("8888888user.phone=" + user.getPhone() + "**********user.password=" + user.getPassword());
//        User user = new User(phone,password);
        try {
            userService.saveUser(user);
            //注册成功，设置跳转信息
            util.setJumpDetails(model, "注册成功！", "返回登录", "../user/login.jsp", 3);
//            JumpDetails jd = new JumpDetails("注册成功！", "返回登录", "../user/login.jsp");
//            req.setAttribute("jumpDetails", jd);
//            return FORWARD + "../jumpPage.jsp";
            return "jumpPage";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error", "系统异常，请稍后重试！");
//            return FORWARD + "../user/register.jsp";
            return "user/register";
        }
    }

    //用户登录操作
    @RequestMapping("/login")
    public String login(Model model, HttpSession session, HttpServletRequest req, String phone, String password) {
        System.out.println("***phone=" + phone + "***password=" + password + "---path=" + req.getSession().getServletContext().getRealPath("/"));
        try {
            User user = userService.findByPhone(phone);
            if (user != null && user.getPassword().equals(password)) {
                session.setAttribute("user", user);
                util.setJumpDetails(model, "登录成功！", "进入个人主页", "../user/userPageJump", 3);
//                return FORWARD + "../jumpPage.jsp";
                return "jumpPage";
            }
            model.addAttribute("error", "账号或密码错误，请重试！");
//            return FORWARD + "../user/login.jsp";
            return "user/login";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error", "系统异常，请稍后重试！");
//            return FORWARD + "../user/login.jsp";
            return "user/login";
        }
    }

    @RequestMapping("/userPageJump")
    public String userPageJump(Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        //进行权限分发
        if (user.getEmploy() == 1) {
            getResume(model, user);
            return "user/onJopPage";//员工页面
        }
        goHomePage(model, user);
        return "user/homePage";//游客页面
    }

    //    @RequestMapping("/goHomePage")
    private void goHomePage(Model model, User user) {
//        User user = (User) session.getAttribute("user");
        ApplySheetQueryVo applySheetQueryVo = new ApplySheetQueryVo();
        applySheetQueryVo.setUser(user);
        applySheetQueryVo.setArranged(1);      //已被公司安排面试
        List<ApplySheet> applySheets = null;
        try {
            applySheets = applyService.findApplyList(applySheetQueryVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        getResume(model, user);
        model.addAttribute("applyNotInterview", applySheets);   //公司已处理未面试的应聘信息
//        return "../user/homePage.jsp";
    }

    private void getResume(Model model, User user) {
        Resume resume = null;
        try {
            resume = userService.findResume(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("resume", resume);
    }

    //保存简历信息
    @RequestMapping("/saveResume")
    public String saveResume(Model model, Resume resume, HttpSession session) {
        //保存简历信息，并转发跳转信息
        User user = (User) session.getAttribute("user");
        resume.setUser(user);
        try {
            userService.saveResume(resume);
            util.setJumpDetails(model, "保存成功！", "进入个人主页", "../user/userPageJump", 3);
        } catch (Exception e) {
            e.printStackTrace();
            util.setJumpDetails(model, "保存失败！", "进入个人主页", "../user/userPageJump", 3);
        }
        return "jumpPage";
    }

    @RequestMapping("/updatePsw")
    public String updatePsw(Model model, HttpSession session, String newPsw) {
        User user = (User) session.getAttribute("user");
        user.setPassword(newPsw);
        try {
            userService.updateUser(user);
            util.setJumpDetails(model, "密码修改成功！", "使用新密码重新登录", "../user/login.jsp", 3);
        } catch (Exception e) {
            e.printStackTrace();
            util.setJumpDetails(model, "密码修改失败！", "重新登录操作", "../user/login.jsp", 3);
        }
        return "jumpPage";
    }


    //获取教育水平列表
    @ModelAttribute("educations")//作用域为request，中间有页面跳转过程会丢失，要使用session进行保存
    public List<Education> getEducations(HttpSession session) {
        List<Education> educations = null;
        try {
            educations = userService.findEducations();
        } catch (Exception e) {
            e.printStackTrace();
        }
//        session.setAttribute("educations",educations);
        return educations;
    }

    //获取政治面貌列表
    @ModelAttribute("politics")
    public List<PoliticsStatus> politicsStatusList(HttpSession session) {
        List<PoliticsStatus> pList = null;
        try {
            pList = userService.findPolitics();
        } catch (Exception e) {
            e.printStackTrace();
        }
//        session.setAttribute("politics",pList);
        return pList;
    }

    // 获取部门列表
    @ModelAttribute("sections")
    public List<Section> sections(HttpSession session) {
        List<Section> sections = null;
        try {
            sections = sectionService.findSections();
        } catch (Exception e) {
            e.printStackTrace();
        }
//        session.setAttribute("sections",sections);
        return sections;
    }

    //获取职位列表
    @RequestMapping("/positions")
    @ResponseBody
    public List<Position> positions(Integer sId) {
//        session.setAttribute("positions",positions);
        return util.getPositions(sId);
    }
}
